﻿/**************************
 * Author: Hientt
 * Date: 17/03/2013
 * Module: 
 * Project: OFB
 **************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;

namespace Advertising.DataAccess.Common
{
    /// <summary> 
    /// This class perform mapping data from SqlDataReader to class properties
    /// </summary>
    public class Mapping
    {
        /// <summary>
        /// Set SqlDataReader to mapping
        /// </summary>
        public static SqlDataReader Reader { get; set; }
        /// <summary> 
        /// This method perform mapping data for a single object
        /// </summary>
        /// <typeparam name="TViewData"></typeparam>
        /// <param name="isNextResult"></param>
        /// <returns></returns>
        public static TViewData MapSingleObject<TViewData>(bool isNextResult = false)
        {
            if (isNextResult)
            {
                if (!Reader.NextResult())
                    return default(TViewData);
            }

            if (!Reader.HasRows)
                return default(TViewData);

            TViewData obj=Activator.CreateInstance<TViewData>();
            if (Reader.Read())
            {
                int totalFields=Reader.FieldCount;
                Type type=typeof(TViewData);

                for (int i=0; i < totalFields; i++)
                {
                    var item=type.GetProperty(Reader.GetName(i));
                    if (item != null)
                    {
                        var objValue=Reader.GetValue(i);
                        var objValueType=objValue.GetType().Name;
                        if (objValueType != "DBNull")
                        {
                            item.SetValue(obj, objValue, null);
                        }
                    }
                }
            }

            return obj;
        }

        /// <summary>
        /// This method perform mapping data for a collection object
        /// </summary>
        /// <typeparam name="TViewData"></typeparam>
        /// <param name="isNextResult"></param>
        /// <returns></returns>
        public static List<TViewData> MapCollectionObject<TViewData>(bool isNextResult = false)
        {
            if (isNextResult)
            {
                if (!Reader.NextResult())
                    return null;
            }

            if (!Reader.HasRows)
                return null;

            List<TViewData> list=new List<TViewData>();
            while (Reader.Read())
            {
                Type type=typeof(TViewData);
                int totalFields=Reader.FieldCount;
                var obj=Activator.CreateInstance<TViewData>();
                for (int i=0; i < totalFields; i++)
                {
                    var item=type.GetProperty(Reader.GetName(i));
                    if (item != null)
                    {
                        var objValue=Reader.GetValue(i);
                        var objValueType=objValue.GetType().Name;
                        if (objValueType != "DBNull")
                        {
                            item.SetValue(obj, objValue, null);
                        }
                    }
                }
                list.Add(obj);
            }

            return list;
        }
    }
}
